package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class CartDaoImpl implements CartDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public int saveCart(Cart cart) throws Exception {
        String sql = "insert into cart(foodId,businessId,userId,quantity) values(?,?,?,1)";
        int result = 0;
        try{
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,cart.getFoodId());
            pst.setInt(2,cart.getBusinessId());
            pst.setString(3,cart.getUserId());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public int updateCart(Cart cart) throws Exception {
        int result = 0;
        String sql = "update cart set quantity=? where userId=? and businessId=? and foodId=?";
        try{
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,cart.getQuantity());
            pst.setString(2,cart.getUserId());
            pst.setInt(3,cart.getBusinessId());
            pst.setInt(4,cart.getFoodId());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }
}
